package com.atguigu.lease.web.admin.custom.converter;

import com.atguigu.lease.model.enums.ItemType;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;

/**
 * 将字符串type='1',type='2'转换为ItemType
 */
@Component
public class StringToItemTypeConverter implements Converter<String, ItemType> {

    @Override
    public ItemType convert(String source) {
        for (ItemType type : ItemType.values()) {
            if (type.getCode().equals(Integer.valueOf(source))) {
                return type;
            }
        }
        throw new IllegalArgumentException("参数不合法-" + source);
    }
}
